package cn.itsource.controller.user;

import cn.itsource.user.domain.User;
import cn.itsource.user.query.UserQuery;
import cn.itsource.service.user.IUserService;
import cn.itsource.utils.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping
    public List<User> select() {
        return userService.selectAll();
    }

    @PutMapping
    public JsonResult update(@RequestBody User user) {
        if (user.id != null) {
            userService.update(user);
            return JsonResult.success("修改成功");
        } else {
            userService.add(user);
            return JsonResult.success("新增成功");
        }
    }

    @PostMapping
    public PageInfo<User> queryPage(@RequestBody UserQuery userQuery) {
        return userService.queryUser(userQuery);
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Integer id) {
        userService.deleteOne(id);
        return JsonResult.success("删除用户数据成功！");
    }

    @PatchMapping
    public JsonResult deleteMore(@RequestBody Long[] id) {

        userService.deleteMore(id);
        return JsonResult.success("删除用户数据成功！");

    }


}
